package Change;

import EncapsulatingObjects.User;
import PersonalHomepage.AdminHomepage;
import PersonalHomepage.UserHomepage;
import Util.Inspectionrules;
import Util.UiUtil;
import Util.UserDatabaseUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class ChangeInfo {
    boolean statue;
    JFrame jf;
    JLabel photo;//头像

    JLabel account;//账号

    JLabel name;//姓名
    JTextField name1;//姓名

    JLabel phone;//电话号码
    JTextField phone1;//电话号码

    JLabel gender;//性别
    JComboBox tgender;

    JLabel birthday;//生日
    JLabel year;
    JLabel month;
    JLabel day;

    JComboBox tyear;
    JComboBox tmonth;
    JComboBox tday;

    JLabel signature;//签名
    JTextArea signature1;//签名

    JButton OK;//退出登录

    UserDatabaseUtil du = new UserDatabaseUtil();
    User user = new User();
    String[] userBirthday;
    UiUtil ui = new UiUtil();

    public ChangeInfo() {
        isAdmin();
        init();
        ui.setFrameCenter(jf);
        jf.setBounds(jf.getX() + 400, jf.getY(), 600, 700);
        jf.setVisible(true);
        ui.setFrameImage(jf);
    }

    private void isAdmin() {
        try {
            Connection conn = du.getConnection();
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from userdata");
            while (rs.next()) {
                if (rs.getBoolean("isLogin") == true) {
                    statue = rs.getBoolean("isAdmin");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initInfo() {
        name1 = new JTextField(user.getName(), JLabel.CENTER);
        name1.setOpaque(true);
        name1.setBackground(new Color(255, 229, 138, 255));
        phone1 = new JTextField(user.getPhone(), JLabel.CENTER);
        phone1.setOpaque(true);
        phone1.setBackground(new Color(255, 229, 138, 255));

        tgender = new JComboBox(new String[]{"Male", "Female"});
        tgender.setSelectedItem(user.getGender());

        userBirthday = user.getBirthday().toString().split("-");

        tyear.setSelectedItem(Integer.parseInt(userBirthday[0]));
        tmonth.setSelectedItem(Integer.parseInt(userBirthday[1]));
        tday.setSelectedItem(Integer.parseInt(userBirthday[2]));

        if ("".equals(user.getSignature()))
            signature1 = new JTextArea("write your personal signature");
        else
            signature1 = new JTextArea(user.getSignature());

        signature1.setLineWrap(true);
        signature1.setOpaque(true);
        signature1.setBackground(new Color(255, 229, 138, 255));
    }

    private void init() {
        jf = new JFrame("USER HOME PAGE");
        jf.setLayout(null);

        photo = new JLabel();

        if (statue)
            UiUtil.setPhoto2(photo);
        else
            UiUtil.setPhoto1(photo);

        name = new JLabel("NAME");
        phone = new JLabel("PHONE");
        gender = new JLabel("GENDER");
        birthday = new JLabel("BIRTHDAY");
        signature = new JLabel("PERSONAL SIGNATURE", JLabel.CENTER);

        year = new JLabel("YEAR");
        month = new JLabel("MONTH");
        day = new JLabel("DAY");

        tyear = new JComboBox();
        tmonth = new JComboBox();
        tday = new JComboBox();
        updateBirthday(tyear, tmonth);
        updataDay(jf, tyear, tmonth, tday);
        tyear.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                updataDay(jf, tyear, tmonth, tday);
                tday.setSelectedItem(Integer.parseInt(userBirthday[2]));
            }
        });
        tmonth.addItemListener(new

                                       ItemListener() {
                                           @Override
                                           public void itemStateChanged(ItemEvent e) {
                                               updataDay(jf, tyear, tmonth, tday);
                                               tday.setSelectedItem(Integer.parseInt(userBirthday[2]));
                                           }
                                       });

        user = du.initUser(user);
        account = new JLabel(user.getAccount(), JLabel.CENTER);
        account.setOpaque(true);
        account.setBackground(new Color(98, 191, 243, 255));

        initInfo();

        OK = new

                JButton("OK");
        OK.setOpaque(true);
        OK.setBackground(new

                Color(245, 132, 132));

        jf.setSize(600, 700);

        photo.setBounds(250, 30, 100, 120);
        photo.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        account.setBounds(250, 160, 100, 20);

        name.setBounds(100, 190, 80, 30);
        name1.setBounds(180, 190, 320, 30);
        phone.setBounds(100, 250, 80, 30);
        phone1.setBounds(180, 250, 320, 30);
        gender.setBounds(100, 310, 80, 20);
        tgender.setBounds(180, 310, 80, 20);
        birthday.setBounds(100, 370, 80, 20);
        tyear.setBounds(180, 370, 60, 20);
        year.setBounds(245, 370, 40, 20);
        tmonth.setBounds(295, 370, 50, 20);
        month.setBounds(350, 370, 50, 20);
        tday.setBounds(415, 370, 50, 20);
        day.setBounds(470, 370, 40, 20);

        signature.setBounds(100, 410, 400, 20);
        signature1.setBounds(100, 435, 400, 100);

        OK.setBounds(225, 565, 150, 40);
        OK.setFont(new

                Font("Harlow Solid Italic", Font.BOLD, 20));
        OK.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (!Inspectionrules.checkTel(jf, user.getPhone()) || !Inspectionrules.checkAccount(jf, user.getAccount())) {
                    return;
                }
                if (JOptionPane.showConfirmDialog(jf, "Are you sure you want to modify the information?") == JOptionPane.YES_OPTION) {
                    user.setName(name1.getText().trim());
                    user.setPhone(phone1.getText().trim());
                    String birthdayDate = tyear.getSelectedItem().toString() + "-" + String.format("%02d", tmonth.getSelectedItem()) + "-" + String.format("%02d", tday.getSelectedItem());
                    DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                    user.setBirthday(LocalDate.parse(birthdayDate, fmt));
                    user.setGender(String.valueOf(tgender.getSelectedItem()));
                    user.setSignature(signature1.getText().trim());
                    du.updateInfo(user);
                    if (JOptionPane.showConfirmDialog(jf, "Information modification succeeded!Do you still want to stay on this page?") == JOptionPane.NO_OPTION) {
                        goBack();
                    }
                }
            }
        });

        jf.add(photo);
        jf.add(account);
        jf.add(name);
        jf.add(name1);
        jf.add(phone);
        jf.add(phone1);
        jf.add(gender);
        jf.add(tgender);
        jf.add(birthday);
        jf.add(year);
        jf.add(tyear);
        jf.add(month);
        jf.add(tmonth);
        jf.add(day);
        jf.add(tday);
        jf.add(signature);
        jf.add(signature1);
        jf.add(OK);

        jf.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                jf.dispose();
                goBack();
            }
        });
    }

    private void goBack() {
        if (statue)
            goAdmin();
        else
            goUser();
    }

    private void goUser() {
        jf.dispose();
        UserHomepage.closePage();
        new UserHomepage();
    }

    private void goAdmin() {
        jf.dispose();
        AdminHomepage.closePage();
        new AdminHomepage();
    }

    private void updataDay(JFrame jf, JComboBox tyear, JComboBox tmonth, JComboBox tday) {
        int years = 1950;
        int months = 1;
        int days = 0;
        Object oYear = tyear.getSelectedItem();
        Object oMonth = tmonth.getSelectedItem();
        if (oYear == null || oMonth == null) {
            return;
        } else {
            years = (int) oYear;
            months = (int) oMonth;
        }

        switch (months) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                days = 30;
                break;
            case 2:
                if ((years % 4 == 0 && years % 100 != 0) || years % 400 == 0) {
                    days = 29;
                } else {
                    days = 28;
                }
                break;
        }
        tday.removeAllItems();
        for (int i = 1; i <= days; i++)
            tday.addItem(i);
    }

    void updateBirthday(JComboBox tyear, JComboBox tmonth) {
        int year = LocalDate.now().getYear();
        for (int i = 1950; i <= year; i++) {
            tyear.addItem(i);
        }
        for (int i = 1; i <= 12; i++) {
            tmonth.addItem(i);
        }
    }
}
